package com.dbky.alg.app;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.authserver.AuthorizationServerTokenServicesConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

/**
 * @Auther: tianchao
 * @Date: 2021/12/16 22:19
 * @Description: AuthorizationServerSecurityConfiguration 发起一个链路
 * OAuth2AuthorizationServerConfiguration 是这个链路发起者的一个默认配置 configure()
 */
@Configuration
@EnableAuthorizationServer
@Import(AuthorizationServerTokenServicesConfiguration.class)
public class AlgAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter implements InitializingBean, ApplicationContextAware {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;


    ApplicationContext applicationContext;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("alg")
                .secret(new BCryptPasswordEncoder().encode("123"))
                .accessTokenValiditySeconds(3600)
                .authorizedGrantTypes("password","refresh_token")
                .scopes("all","read","write");
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AuthorizationServerTokenServicesConfiguration bean = applicationContext.getBean(AuthorizationServerTokenServicesConfiguration.class);
        System.out.println(bean);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
